<?php
namespace App\Admin\Controllers\System;
use App\Lib\Classes\AcAutomation;
use App\Wen\Utils\SearchUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class AdminSearchController extends AdminController{

    public function clear(Request $request){
        SiteUtils::forget_config_data();
        if(env('APP_URL') != 'https://test.minisns.cn'){
            Cache::forget('__MINISNS_VERSION__');
            Cache::forget('total:todo:count');
            Cache::forget('total:tenant:todo:count');
            Cache::forget('system:remote:version:tip');
        }
        return JsonResponse::make()->success('清理完毕');
    }

    public function search(Request $request)
    {
        $search_content = $request->search_content;
        if(_empty_($search_content)){
            return JsonResponse::make()->html('<div class="search_items">搜索结果为空</div>')->success('搜索完毕');
        }

        $res = Http::post('https://mini.minisns.cn/api/v1/three/search_menu', ['search_content'=>$search_content]);
        $search_arr = null;
        if($res->successful()){
            $search_arr = $res->json();
        }

        $html = '';
        $actree = new AcAutomation(SearchUtils::split_keywords($search_content));
        $pre_tags = '<span style="color: #fc3a72;">';
        $end_tag =   '</span>';

        if($search_arr && $search_arr['status']){
            if($search_arr['data'] && count($search_arr['data']) > 0){
                foreach ($search_arr['data'] as $item){
                    if($item['menu']){
                        $uri = admin_url(ltrim($item['menu']['uri'], '/'));
                        if($item['menu_id'] == $item['parent_id']){
                            $text = $item['menu']['title'] . ' - ' . $item['keyword'];
                        }else{
                            $text = $item['parent']['title'] . ' - ' . $item['menu']['title'] . ' - ' . $item['keyword'];
                        }

                        $ac_res = $actree->search($text);
                        $tmp_text = $text;
                        $replaced = [];
                        if($ac_res && is_array($ac_res)){
                            foreach ($ac_res as $pos) {
                                $searched_part = mb_substr($tmp_text, $pos[0],$pos[1] - $pos[0],'UTF-8');
                                if(in_array($searched_part, $replaced)){
                                    continue;
                                }else{
                                    $replaced[] = $searched_part;
                                }
                                $text = str_replace($searched_part, $pre_tags.$searched_part.$end_tag, $text);
//                                $text = str_replace($searched_part, $pre_tags.$searched_part.$end_tag, $text);
                            }
                        }


                        $html .= '<div class="search_items" onclick="search_reload(\''.$uri.'\', \''.$tmp_text.'\')">'.$text.'</div>';
                    }
                }
            }else{
                return JsonResponse::make()->html('<div class="search_items">搜索结果为空</div>')->success('搜索完毕');
            }
        }

        return JsonResponse::make()->html($html)->success('搜索完毕');
    }
}
